<?php

namespace app\common\model;

use think\Model;

class Goods extends Model
{
    // 表名
    protected $name = 'goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'sale_text',
	    'hot_text',
	    'new_text',
	    'recommend_text'
    ];

    const SALE_Y = 1;
	const SALE_N = 0;
	
	public static $_sale = [
		self::SALE_Y => '是',
		self::SALE_N => '否',
	];
	
	public function getSaleTextAttr($value, $data) {
		$value = $value ? $value : $data['is_sale'];
		$list = self::$_sale;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	const HOT_Y = 1;
	const HOT_N = 0;
	public static $_hot = [
		self::HOT_Y => '是',
		self::HOT_N => '否',
	];
	public function getHotTextAttr($value, $data) {
		$value = $value ? $value : $data['is_hot'];
		$list = self::$_hot;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	const RECOMMEND_Y = 1;
	const RECOMMEND_N = 0;
	public static $_recommend = [
		self::RECOMMEND_Y => '是',
		self::RECOMMEND_N => '否',
	];
	
	public function getRecommendTextAttr($value, $data) {
		$value = $value ? $value : $data['is_recommend'];
		$list = self::$_recommend;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	const NEW_Y = 1;
	const NEW_N = 0;
	public static $_new = [
		self::NEW_Y => '是',
		self::NEW_N => '否',
	];
	
	public function getNewTextAttr($value, $data) {
		$value = $value ? $value : $data['is_new'];
		$list = self::$_new;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	
	public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }


    public function category(){
		return $this
			->belongsTo('GoodsCategory', 'goods_category_id', 'id', '', 'left')
			->setEagerlyType(0);
    }
    
    public function brand(){
		return $this->belongsTo('Brand', 'brand_id', 'id', '', 'left')->setEagerlyType(0);
    }
    
    
}
